layui.use(['element', 'layer', 'form', 'table','laydate','upload'], function () {
    var element = layui.element;
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var upload = layui.upload;
    var sex=$('#inputsex').val();
    $('[name="d_sex"][value="' + sex + '"]').prop('checked', true);//改变radio选择
    form.render();
    form.on('submit(updateUserBtn)', function(data){
        var params = $.param({
            id:data.field.d_id,
            name: data.field.d_name,
            sex: data.field.d_sex,
            age: data.field.d_age,
            phone: data.field.d_phone,
            mail: data.field.d_email,
            idCard: data.field.d_idCard,
            address: data.field.d_address
        }, true);
        $.ajax({
            url: '/front/myCenter/person/updatePersonalData',
            type: 'post',
            data: params,
            dataType: 'json',
            success: function (e) {
                layer.msg(e.msg);
                if (e.msg == "修改成功") {
                    setTimeout(function () {
                        window.location.href='/front/myCenter/person/personalData';
                    },1000);
                }
            }
        });
        return false;
    });
    form.on('switch(isUpdate)', function(data){
        if (data.elem.checked) {
            $('#detail_upload_view').show();
            $('#updateBtnView').show();
        }else {
            $('#detail_upload_view').hide();
            $('#updateBtnView').hide();
        }
    });

    upload.render({
        elem:'#d_uploadMyphoto',
        url:'/front/myCenter/person/upload',
        accept:'images',
        auto: false,
        bindAction:'#d_confirmMyphoto',
        choose:function(obj) {
            obj.preview(function(index, file, result){
                $('#d_showphoto').attr('src', result);
            });
        },
        done: function(res, index, upload){
            if (res.code != 0)
                layer.msg('上传失败');
            else {
                layer.msg('上传成功');
                $('#showMyPhoto').attr('src', res.data.access_url);
            }
        }
    })

    var active = {
        updatePassword:function () {
            var password = $('#newpassword').val();
            var oldpassword = $('#oldpassword').val();
            if (password == '' && oldpassword== '') {
                layer.msg("未做任何修改");
                return false;
            }
            var params = $.param({
                password:password,
                oldPassword:oldpassword
            }, true);
            $.ajax({
                url: '/front/myCenter/person/updatePwd',
                type: 'post',
                data: params,
                dataType: 'json',
                success: function (e) {
                    layer.msg(e.msg);
                    if (e.msg == "密码修改成功,请重新登录")
                        setTimeout(function () {
                            window.location.href='/front/myCenter/person/updatePwdView';
                        },1000);
                }
            })
        }
    };
    $('.updatePassword .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
});